import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.atguigu.commonutils.RandomUtil;
import com.atguigu.msmservice.utils.ConstantPropertiesUtil;
import org.junit.Test;

import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

public class testMsm {

    /*阿里云短信测试*/
        @Test
        public void test() throws Exception {
            String accessKeyId="LTAI5tQchYebZzdABmtNB2vU";
            String accessKeySecret="kPF9GbrkJJE09ybEUgWSaxBursPUIq";
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    // 您的 AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 您的 AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            Client client = new Client(config);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("code", "123456");
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName("阿里云短信测试")
                    .setTemplateCode("SMS_154950909")
                    .setPhoneNumbers("19938164397")
//            "{\"code\":\"1234\"}"
                    .setTemplateParam(JSONObject.toJSONString(hashMap));
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendSmsWithOptions(sendSmsRequest, runtime);
            } catch (TeaException error) {
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
            }
        }

        @Test
        public void test2(){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("code", "123456");
            String s = JSONObject.toJSONString(hashMap);
            System.out.println("code"+s);
        }


        /*注意这里在导入静态变量的时候，ConstantPropertiesUtil不能使用，因为这个类是交给spring进行管理的*/
        @Test
        public void test3() throws Exception {
            /*1.设置id和秘钥*/
            Config config = new Config().setAccessKeyId("LTAI5tQchYebZzdABmtNB2vU")
                    .setAccessKeySecret("kPF9GbrkJJE09ybEUgWSaxBursPUIq");
            config.endpoint="dysmsapi.aliyuncs.com";
            /*2.根据config创建client*/
            Client client = null;
            try {
                client = new Client(config);
            } catch (Exception e) {
                e.printStackTrace();
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", "1234");
            String code = JSONObject.toJSONString(map);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest =
                    new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers("19938164397")
                    .setSignName("阿里云短信测试")
                    .setTemplateCode("SMS_154950909")
                    .setTemplateParam(code);

            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendSmsWithOptions(sendSmsRequest, runtime);
            } catch (TeaException error) {
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
            }
        }

}










